package topcbl.task;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import topcbl.data.Driver;
import topcbl.net.TaxiService;
import topcbl.taxi.MainActivity;
import topcbl.taxi.R;
import topcbl.taxi.TaxiDriverDialog;
import topcbl.util.StringUtils;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class GetCarInfoTask extends AsyncTask<Void, Void, Driver> {
	Context context;
	String strTEID;
	ProgressDialog pDialog;
	TaxiService mService;

	public GetCarInfoTask(Context context, String strTEID) {
		this.context = context;
		this.strTEID = strTEID;
		pDialog = ProgressDialog.show(context, "", context.getString(R.string.wait_network));
		mService = MainActivity.mBindService.getService();
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		// show progress dialog
		pDialog.setCanceledOnTouchOutside(true);
		pDialog.setCancelable(true);
		pDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				cancel(true);
			}
		});
	}

	@Override
	protected Driver doInBackground(Void... params) {
		HttpClient httpclient = new DefaultHttpClient();
		String url = "http://gps.smartsafe.vn/taxiws/Service1.svc/GetCarsInfo/"
				+ strTEID;
		HttpResponse response;
		try {
			response = httpclient.execute(new HttpGet(url));
			String data = StringUtils.readAllFromInputStream(response
					.getEntity().getContent());
			return parseJSON(data);
		} catch (ClientProtocolException e) {
			Log.e("topcbl", e.getMessage());
		} catch (IOException e) {
			Log.e("topcbl", e.getMessage());
		}
		return null;
	}

	@Override
	protected void onPostExecute(Driver result) {
		pDialog.dismiss();
		if (result != null) {
			updateUI(result);
		} else {
			Toast.makeText(context,
					context.getString(R.string.network_unavailable),
					Toast.LENGTH_LONG).show();
		}
		super.onPostExecute(result);
	}

	@Override
	protected void onCancelled() {
		pDialog.dismiss();
		super.onCancelled();
	}

	private Driver parseJSON(String data) {
		try {
			JSONArray arr = new JSONArray(data);
			JSONObject obj = arr.getJSONObject(0);
			return new Driver(obj.getString("strIconID"),
					obj.getString("strCarNum"), obj.getString("strOwnerName"),
					obj.getString("strOwnerTel"),
					obj.getString("strOwnerAddress"));

		} catch (Exception e) {
			Log.e("topcbl", "Fail to parse JSON " + e.getMessage());
		}
		return null;
	}

	public void updateUI(Driver driver) {
		TaxiDriverDialog dialog = new TaxiDriverDialog(context);
		dialog.setContent(driver.getStrOwnerName(), driver.getStrCarNum(),
				driver.getStrOwnerTel());
		dialog.show();
	}
}
